<?php

require 'firebaseLib.php';
require 'firebase.php';

$action = $_POST['action'];

if ($action == 'sendMoney') {
	// Confirm value is positive
	$value = $_POST['value'];
	if ($value <= 0) die();

	// Confirm both users exist
	$id = $_POST['id'];
	$receiverID = $_POST['receiverID'];
	if (!doesUserExist($id) || !doesUserExist($receiverID)) die();

	// Confirm user has enough money
	$myBalance = getUserBalance($id);
	if ($value > $myBalance) die();

	// Authenticate token
	$token = $_POST['token'];
	if (! authorizeToken($token, $id)) die();

	// Exchange currency
	$receiverBalance = getUserBalance($receiverID);
	$myBalance -= $value;
	$receiverBalance += $value;
	setUserBalance($id, $myBalance);
	setUserBalance($receiverID, $receiverBalance);
}